package cn.itcast.com.homework.day815;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Random;

/*
1. 定义一个Student类, 用于存储学生的姓名 ，学号，以及  Java语言，数据库， Spring三门课程的成绩，以及总分。
2. 初始化50个Student对象，随机生成 三门课程成绩，范围为0-100分。并计算总分。将学生放入一个ArrayList集合中。
3. 将ArrayList里的学生按照总分从大到小进行排序。排序完成后遍历输出。
 */
public class test {
    private static int beginNum = 0;

    public static void main(String[] args) {
        ArrayList<Object> student = createStudent(50);
        for (int i = 0; i < 50; i++) {
            Student o = (Student) student.get(i);
            System.out.print("Name "+o.getName());
            System.out.print(" Num "+o.getNum());
            System.out.print(" JavaScor "+o.getJavaScore());
            System.out.print(" DatabasrScor "+o.getDatabasrScore());
            System.out.print(" SpringScore "+o.getSpringScore());
            System.out.print(" TotalScore "+o.getTotalScore());
            System.out.println(" ");
        }
        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSS");
        qsort(student);
        for (int i = 0; i < 50; i++) {
            Student o = (Student) student.get(i);
            System.out.print("Name "+o.getName());
            System.out.print(" Num "+o.getNum());
            System.out.print(" JavaScor "+o.getJavaScore());
            System.out.print(" DatabasrScor "+o.getDatabasrScore());
            System.out.print(" SpringScore "+o.getSpringScore());
            System.out.print(" TotalScore "+o.getTotalScore());
            System.out.println(" ");
        }
    }
    public static void qsort(ArrayList studet){
        //设置起点和终点
        int begin = 0;
        int end = studet.size()-1;
        for (int i = 0; i < studet.size(); i++) {
            //声明最大值的位置
            int max = i;
            for (int r = begin; r < studet.size(); r++) {
                //强转找到对象，通过对象的get进行比较
                Student student = (Student) studet.get(r);
                Student studentMAX = (Student) studet.get(max);
                //假如这个循环中的对象的总分比max高，就将max的值置为r
                if (student.getTotalScore() > studentMAX.getTotalScore()){
                    max = r;
                }
            }
            //一轮循环找到当前的最大值之后，进行交换
            Student temp = new Student("0", 0, 0, 0, 0, 0);
            temp = (Student) studet.get(i);
            studet.set(i,studet.get(max));
            studet.set(max,temp);
            //每进行一次排序，游标右移一位
            begin++;
        }

    }


    public static ArrayList<Object> createStudent(int k){
        ArrayList<Object> objects = new ArrayList<>();
        for (int i = 0; i < k; i++) {
                Random random = new Random();
                //使用随机数0-2 加上1 防止为0
                String studentName = getRandomJianHan(random.nextInt(2)+1);
                int studentNumber  = beginNum++;
                int studentJavaScore = random.nextInt(100);
                int studentDatabaseScore = random.nextInt(100);
                int studentSpringScore = random.nextInt(100);
                int studentTotalScore = studentDatabaseScore + studentJavaScore + studentSpringScore;
                Student student = new Student(studentName, studentNumber, studentJavaScore, studentDatabaseScore, studentSpringScore, studentTotalScore);
                objects.add(student);
        }
        return objects;
    }


    //网上找的代码，forfun自动生成名字（中文）
    public static String getRandomJianHan(int len) {
        String ret = "";
        for (int i = 0; i < len; i++) {
            String str = null;
            int hightPos, lowPos; // 定义高低位
            Random random = new Random();
            hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值
            lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值
            byte[] b = new byte[2];
            b[0] = (new Integer(hightPos).byteValue());
            b[1] = (new Integer(lowPos).byteValue());
            try {
                str = new String(b, "GBK"); // 转成中文
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            ret += str;
        }
        return ret;
    }

    public static void quickSort(int[] arr,int low,int high){
        int i,j,temp,t;
        if(low>high){
            return;
        }
        i=low;
        j=high;
        //temp就是基准位
        temp = arr[low];

        while (i<j) {
            //先看右边，依次往左递减
            while (temp<=arr[j]&&i<j) {
                j--;
            }
            //再看左边，依次往右递增
            while (temp>=arr[i]&&i<j) {
                i++;
            }
            //如果满足条件则交换
            if (i<j) {
                t = arr[j];
                arr[j] = arr[i];
                arr[i] = t;
            }

        }
        //最后将基准为与i和j相等位置的数字交换
        arr[low] = arr[i];
        arr[i] = temp;
        //递归调用左半数组
        quickSort(arr, low, j-1);
        //递归调用右半数组
        quickSort(arr, j+1, high);
    }
}

class Student{
    String name;
    int num;
    int JavaScore;
    int DatabasrScore;
    int SpringScore;
    int totalScore = JavaScore + DatabasrScore + SpringScore;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getJavaScore() {
        return JavaScore;
    }

    public void setJavaScore(int javaScore) {
        JavaScore = javaScore;
    }

    public int getDatabasrScore() {
        return DatabasrScore;
    }

    public void setDatabasrScore(int databasrScore) {
        DatabasrScore = databasrScore;
    }

    public int getSpringScore() {
        return SpringScore;
    }

    public void setSpringScore(int springScore) {
        SpringScore = springScore;
    }

    public int getTotalScore() {
        return totalScore;
    }

    public void setTotalScore(int totalScore) {
        this.totalScore = totalScore;
    }

    public Student(String name, int num, int javaScore, int databasrScore, int springScore, int totalScore) {
        this.name = name;
        this.num = num;
        this.JavaScore = javaScore;
        this.DatabasrScore = databasrScore;
        this.SpringScore = springScore;
        this.totalScore = totalScore;
    }
}
